﻿using System;
using System.Data;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace SLS.ExClassLib.Message
{
    [Serializable]
    [MessageContract]
    public class MessageEx
    {
        //Constructor - create an empty message.
        
        public override string ToString()
        {
            string MessageStr = "";
            MessageStr += "Agent:" + this.AgentID + ";";
            MessageStr += "CallerPath:" + CallerPath + ";";
            MessageStr += "Address:" + Address + ";";
            MessageStr += "JobID:" + JobID + ";"; 
            MessageStr += "JobContent:" + JobContent + ";"; 
            return MessageStr;
        }
        public string DataSet2String(DataSet dsSource)
        {
            string DataSetStr = "";
            if (dsSource == null)
            {
                return "DataSet is null";
            }
            foreach (DataTable dtTemp in dsSource.Tables)
            {
                DataSetStr = "Tables: " + dtTemp + ";";
                int i = 0;
                foreach (DataRow dwTemp in dtTemp.Rows)
                {
                    i++;
                    DataSetStr += "Line " + i.ToString() + ":";
                    DataSetStr += ";";
                    foreach (DataColumn dwColumn in dtTemp.Columns)
                    {
                        DataSetStr += dwColumn.ColumnName + ":" + dwTemp[dwColumn.ColumnName].ToString().Trim();
                        DataSetStr += ";";
                    }
                    DataSetStr += ";";
                }
            }
            return DataSetStr;
        }
        public MessageEx()
        {
            this.AgentID = "1";
            this.Address = "";
            this.Federation = "";
            this.CallerPath = "";
            this.CallerUID = "";
            this.MessageID = Guid.NewGuid().ToString(); 
            this.JobID = null;
            this.JobContent = "";
            this.SM = null; 
            this.setEvent = new DataSet(); 
            this.JobType = null;
            this.Address = ""; 
            this.Tag = "NO";
            this.Status = "New";
            this.MessageID_Req = "";
        }
        public MessageEx(string _Address, string _Federation, string _JobID, string UpdateFedTime)
        {
            this.AgentID = "1";
            this.Address = _Address;
            this.Federation = _Federation;
            this.CallerPath = "";
            this.CallerUID = "";
            this.MessageID = Guid.NewGuid().ToString();
            this.MessageID_Req = "";
            this.JobID = _JobID;
            this.JobContent = "";
            this.SM = null;
            this.setEvent = new DataSet(); 
            this.JobType = null;  
            this.Tag = UpdateFedTime;
            this.Status = "New"; 
        } 
        public MessageEx(MessageEx msg, bool WithDataSet=true)
        {
            if (WithDataSet)
            { 
                this.setEvent = msg.setEvent; 
            }
            else
            {
                this.setEvent = new DataSet(); 
            }
            this.AgentID = msg.AgentID;
            this.Address = msg.Address;
            this.Federation = msg.Federation;
            this.CallerPath = msg.CallerPath;
            this.CallerUID = msg.CallerUID; 
            this.MessageID = Guid.NewGuid().ToString();
            this.MessageID_Req = "";
            this.JobID = msg.JobID;
            this.SM = msg.SM; 
            this.JobType = msg.JobType; 
            this.JobContent = msg.JobContent;    
            this.Tag = msg.Tag;
            this.Status = "New"; 
        } 
        #region=================Property=================
        [MessageHeader]
        public string AgentID
        {
            get;
            set;
        }
        [MessageHeader]
        public string Address
        {
            get;
            set;
        }
        [MessageHeader]
        public string Federation
        {
            get;
            set;
        }
        [MessageHeader]
        public DateTime TimeStamp
        {
            get;
            set;
        }
        [MessageHeader]
        public string CallerPath
        {
            get;
            set;
        }
       [MessageHeader]
        public string CallerUID
        {
            get;
            set;
        }
       [MessageHeader]
        public string SM
        {
            get;
            set;
        }
        [MessageHeader]
        public string MessageID
        {
            get;
            set;
        }
      [MessageHeader]
        public string MessageID_Req
        {
            get;
            set;
        } 
        [MessageHeader]
        public string Status
        {
            get;
            set;
        } 
        [MessageHeader]
        public object Tag
        {
            get;
            set;
        }   
        [MessageBodyMember]
        public string JobType
        {
            get;
            set;
        }
        [MessageBodyMember]
        public string JobID
        {
            get;
            set;
        }
        [MessageBodyMember]
        public string JobContent
        {
            get;
            set;
        }
        [MessageBodyMember]
        public DataSet setEvent
        {
            get;
            set;
        }  
        #endregion
    }
}